package com.qzboot.backend.web.common.config;

import com.qzboot.common.support.BusinessException;
import com.qzboot.common.support.WebResult;
import com.qzboot.facade.common.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;

/**
 * @author zengzhihong
 * @date 2019-12-15
 */
@ResponseStatus
@Slf4j
@RestControllerAdvice
public class WebExceptionHandler {

    @ExceptionHandler(IllegalArgumentException.class)
    public WebResult<?> handleIllegalArgumentException(IllegalArgumentException e) {
        log.error(e.getMessage(), e);
        return WebResult.error(e.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    public WebResult<?> handleBusinessException(BusinessException e) {
        log.error(e.getMessage(), e);
        return WebResult.error(e.getMessage());
    }

    @ExceptionHandler(ServiceException.class)
    public WebResult<?> handleServiceException(ServiceException e) {
        log.error(e.getMessage(), e);
        return WebResult.error(e.getMessage());
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public WebResult<?> handleDuplicateKeyException(DuplicateKeyException e) {
        log.error(e.getMessage(), e);
        return WebResult.error("数据已存在,请检查提交的数据");
    }


    @ExceptionHandler(AuthorizationException.class)
    public WebResult<?> handleAuthorizationException(AuthorizationException e) {
        log.error(e.getMessage(), e);
        return WebResult.error("没有权限，请联系管理员授权");
    }

    @ExceptionHandler(ShiroException.class)
    public WebResult<?> handleUnauthorizedException(ShiroException e) {
        log.error(e.getMessage(), e);
        return WebResult.error(e.getMessage());
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public WebResult<?> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        return WebResult.error(String.format("不支持%s请求", e.getMethod()));
    }

    @ExceptionHandler(BindException.class)
    public WebResult handleBindException(BindException e) {
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        return WebResult.error(allErrors.get(allErrors.size() - 1).getDefaultMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public WebResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        return WebResult.error(allErrors.get(allErrors.size() - 1).getDefaultMessage());
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public WebResult handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        return WebResult.error("请求参数错误");
    }

    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public WebResult handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
        return WebResult.error(String.format("不支持%s请求", e.getContentType()));
    }

    @ExceptionHandler(Exception.class)
    public WebResult<?> handleException(Exception e) {
        log.error(e.getMessage(), e);
        return WebResult.error("系统繁忙");
    }
}
